package com.sunrise.xdoc.view.catalog;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import com.sunrise.xdoc.entity.catalog.LoaiSanPham;
import com.sunrise.xdoc.entity.config.ChiNhanh;
import com.sunrise.xdoc.view.AbstractView;

@ManagedBean
@ViewScoped
public class LoaiSanPhamView extends AbstractView<LoaiSanPham> {
	private static final long serialVersionUID = 9160550591813254187L;

	private List<ChiNhanh> chiNhanhs = new ArrayList<>();

	@Override
	@PostConstruct
	protected void init() {
		super.init();
		chiNhanhs = getDaoHelper().getDao(ChiNhanh.class).loadAll();
	}

	public Long getSelectedChiNhanhId() {
		if (getEditedEntity() != null && getEditedEntity().getChiNhanh() != null) {
			return getEditedEntity().getChiNhanh().getId();
		}
		return null;
	}

	public void setSelectedChiNhanhId(Long selectedChiNhanhId) {
		for (ChiNhanh cn : chiNhanhs) {
			if (selectedChiNhanhId.equals(cn.getId())) {
				getEditedEntity().setChiNhanh(cn);
				return;
			}
		}
		getEditedEntity().setChiNhanh(null);
	}

	@Override
	protected String getDetailPanelViewId() {
		return "loaiSanPhamDetailFormId:loaiSanPhamPanelGridId";
	}

	@Override
	protected String getDataModelBeanName() {
		return "loaiSanPhamDataModel";
	}

	public List<ChiNhanh> getChiNhanhs() {
		return chiNhanhs;
	}
}
